package com.iqiyi.config;


import com.iqiyi.util.RedisUtilSerializer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 *
 *  Redis连接配置
 * @author YoungKai
 * @date 2021/2/11 18:26
 */
@Configuration
public class RedisConfig {

    //获取Jedis链接工程对象
    @Bean(name = "jedisConnectionFactory")
    public JedisConnectionFactory getConnectionFactory(){
        return new JedisConnectionFactory();
    }


    //创建redisTemplate模板对象
    @Bean
    public RedisTemplate<String,Object> redisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory jedisConnectionFactory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
//        redisTemplate.setConnectionFactory(getConnectionFactory());
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
//        redisTemplate.setKeySerializer(new StringRedisSerializer()); //添加字符串序列化器
//        redisTemplate.setValueSerializer(new StringRedisSerializer()); //添加字符串value反序列化器
        redisTemplate.setKeySerializer(new RedisUtilSerializer());   //添加自定义的序列化器
        redisTemplate.setValueSerializer(new RedisUtilSerializer());
        return redisTemplate;
    }

}
